package com.djk.pojo;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 统一响应数据
 */
@Data
@NoArgsConstructor
public class Result<T> {

    private Integer code;//响应码
    private String msg;//响应信息描述
    private T data;//响应数据

    public static <T> Result<T> build(T data, ResultCodeEnum resultCodeEnum) {
        Result<T> result = build(data);
        result.setCode(resultCodeEnum.getCode());
        result.setMsg(resultCodeEnum.getMsg());
        return result;
    }

    /**
     * 响应成功
     * @param data
     * @return
     * @param <T>
     */
    public static <T> Result<T> ok(T data) {
        return build(data, ResultCodeEnum.SUCCESS);
    }
    public static <T> Result<T> error(T data){
        return build(data,ResultCodeEnum.ERROR);
    }
    public static <T> Result<T> build(T body, Integer code, String message) {
        Result<T> result = build(body);
        result.setCode(code);
        result.setMsg(message);
        return result;
    }

    protected static <T> Result<T> build(T data) {
        Result<T> result = new Result<>();
        if (data != null)
            result.setData(data);
        return result;
    }


}
